package com.canton.dao;

import com.canton.dao.entity.GrapeNode;
import com.canton.dao.entity.ImageNode;
import com.canton.dao.repository.GrapeRepository;
import com.canton.dao.repository.ImageRepository;
import com.canton.service.GrapeService;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@WebAppConfiguration

class node {

    @Autowired
    private WebApplicationContext wac;
    @Autowired
    private MockMvc mockMvc;


    //模拟controller层
    @Before
    public void setUp (){
//        mockMvc = MockMvcBuilders.standaloneSetup(new helloController()).build();
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();//建议使用这种
    }

    @Test
    public void testController() throws Exception {
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/knowledgeSearch/getkeyword?keyword='长相思'").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();

        String content=mvcResult.getResponse().getContentAsString();    //得到返回结果
        System.out.println("content"+content);
    }
    @Autowired
    private GrapeRepository grapeRepository;
    private final static Logger log = LoggerFactory.getLogger(node.class);

    @Resource
    private ImageRepository imageRepository;

    @Test
    public void testPerson(){

//        Collection<ImageNode> allLimitBy = imageRepository.findAllLimitBy("丰泰佳",10);
        List<GrapeNode> byNameLike = grapeRepository.findByNameLike("长相思");
        System.out.println(byNameLike);

    }
}
